unit uCFS;

interface

function CfsEnCode(codeStr: string): string;

implementation

uses
  SysUtils;
  
function CfsCode(const wordStr: string): string;
var
  i: Integer;
  str: string;
begin
  str := '';
  result := '';
  for i := 1 to Length(wordStr) do
  begin
    str := str + IntToStr(ord(wordStr[i]));
  end;
  result := IntToHex(StrToInt(str), 0);
end;

function CfsEnCode(codeStr: string): string;
var
  CodeLen, CodeSpace, Been: Integer;
  i: Integer;
  NewCode: Double;
  newCodeStr: string;
begin
  result := '';
  CodeLen := 32;
  CodeSpace := CodeLen - Length(CodeStr);

  if not CodeSpace < 1 then
  begin
    for i := 1 to CodeSpace do
      codeStr := codeStr + #21;
  end;

  NewCode := 1;
  for i := 1 to CodeLen do
  begin
    Been := CodeLen + ord(CodeStr[i]) * i;
    NewCode := NewCode * Been;
  end;


  CodeStr := Format('%8.2f', [NewCode]);
  CodeStr := FloatToStr(NewCode);
  CodeStr := StringReplace(codeStr, 'E', 'E+', [rfReplaceAll]);
  NewCode := 0;
  newCodeStr := '';

  for i := 1 to Length(CodeStr) do
  begin
    if i+2 > Length(CodeStr) then
      newCodeStr := newCodeStr + CfsCode(Copy(CodeStr, i, Length(CodeStr)-i + 1))
    else
      newCodeStr := newCodeStr + CfsCode(Copy(CodeStr, i, 3));
  end;

  i := 20;
  while i <= Length(newCodeStr) - 18  do
  begin
    result := result + newCodeStr[i];
    inc(i, 2);
  end;
end;
end.
 